package com.coy.web.config;

import com.coy.web.common.dto.DataResult;
import com.coy.web.common.exception.BizException;
import com.coy.web.common.exception.BizResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public DataResult handleException(Exception e) {
        log.error("#系统全局异常捕获#", e);
        return DataResult.error(BizResultCode.ERR_SYSTEM.getCode(), "系统繁忙，请稍后再试！", e.getMessage());
    }

    @ExceptionHandler(BizException.class)
    @ResponseBody
    public DataResult handleBizException(BizException e) {
        return DataResult.error(e.getCode(), e.getMsg());
    }
}
